local desc_oddfight_mode = [[
  # 镜像模式简介
  ___
    
  镜像对战

  胜利规则与身份局一致。双方分别使用相同武将对战。开启副将则进入全体镜像模式
]]
local kuanglang_getLogic = function()
  local kuanglang_logic = GameLogic:subclass("jbs__mirror_logic")

  
  function kuanglang_logic:assignRoles()
    local room = self.room
    local n = #room.players
    local roles = table.random {
      { "loyalist", "rebel", "rebel", "loyalist" , "loyalist",  "rebel", "rebel","loyalist"},
      { "rebel", "loyalist", "loyalist", "rebel","rebel", "loyalist" , "loyalist","rebel"},
    }

  
    for i = 1, n do
      local p = room.players[i]
      p.role = roles[i]
      p.role_shown = true
      room:broadcastProperty(p, "role")
    end
    self.start_role = roles[1]

    room.players[1].role = "lord"

    -- for adjustSeats
    -- for adjustSeats
  end
  
  
  function kuanglang_logic:chooseGenerals()
    local room = self.room
    local generalNum = room:getSettings('generalNum')

    local lord = room:getLord()
    room.current = lord
    lord.role = self.start_role

    local nonlord = room.players
    local generals = Fk:getGeneralsRandomly(generalNum*2)
    table.shuffle(generals)
    local arg = {}
      for i = 1, generalNum do
        table.insert(arg, table.remove(generals, 1).name)
      end
    for _, p in ipairs(nonlord) do
      p.request_data = json.encode({ arg, 1 })
      p.default_reply = arg[1]
    end

    room:doBroadcastRequest("AskForGeneral", nonlord)
    if not room:getSettings('enableDeputy')  then
      local lit={}
      lit[1]={}
      lit[2]={}
      for _, p in ipairs(nonlord) do
          if p.role=="rebel" then
            table.insert(lit[1],p)
          elseif p.role=="loyalist" then
             table.insert(lit[2],p)
          end
      end

      local p=table.random(lit[1])
      local v=table.random(lit[2])
     local general1 =  (p.general == "" and p.reply_ready and json.decode(p.client_reply)[1]) or p.default_reply
     local general2 =  (v.general == "" and v.reply_ready and json.decode(v.client_reply)[1]) or v.default_reply


      for _, p in ipairs(nonlord) do
       
      room:setPlayerGeneral(p, (p.role=="rebel" and general1) or (p.role=="loyalist" and general2), true, true)
      p.default_reply = ""
      end
    else
      local lit={}
      for _, p in ipairs(nonlord) do
        if p.id>0 then
          table.insert(lit,p)
        end
      end
      local p=table.random(lit)
      local general =  (p.general == "" and p.reply_ready and json.decode(p.client_reply)[1]) or p.default_reply
      for _, p in ipairs(nonlord) do
       
        room:setPlayerGeneral(p, general, true, true)
        p.default_reply = ""
      end

    end
    room:askForChooseKingdom(nonlord)
  end
  return kuanglang_logic
end

local odd_rule = fk.CreateTriggerSkill{
  name = "#mirrorfight_rule",
  priority = 0.001,
  refresh_events = {fk.DrawInitialCards, fk.DrawNCards, fk.GameOverJudge, fk.Deathed},
  can_refresh = function(self, event, target, player, data)
    return target == player
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    if event == fk.DrawNCards then
      if player.seat == 1 and player:getMark(self.name) == 0 then
        room:addPlayerMark(player, self.name, 1)
        room:setTag("SkipNormalDeathProcess", true)
        data.n = data.n - 1
      end
    elseif event == fk.DrawInitialCards then
      if player.seat >= 4 then
        data.num = data.num + 1
      end
    elseif event == fk.GameOverJudge then
      local winner = Fk.game_modes[room:getSettings('gameMode')]:getWinner(player)
      if winner ~= "" then
        room:gameOver(winner)
        return true
      end
    else
      for _, p in ipairs(room.alive_players) do

        if p.role == player.role then
          p:drawCards(2)
        end
      end
    end
  end,
}


Fk:addSkill(odd_rule)


local kuanglang_mode = fk.CreateGameMode{
  name = "mirror_mode",
  minPlayer = 4,
  maxPlayer = 8,
  rule = odd_rule,
  logic = kuanglang_getLogic,

  surrender_func = function(self, playedTime)
    local surrenderJudge = { { text = "time limitation: 2 min", passed = playedTime >= 1 },
    { text = "2v2: left you alive", passed =
     not table.find(Fk:currentRoom().alive_players, function(p)
      return p~=Self and p.role == Self.role
    end) and true } }
    return surrenderJudge
  end,
  winner_getter = function(self, victim)
    local room = victim.room
    local alive = table.filter(room.alive_players, function(p)
      return not p.surrendered
    end)
    local winner = alive[1].role
    for _, p in ipairs(alive) do
      if p.role ~= winner then
        return ""
      end
    end
    return winner
  end,
}


Fk:loadTranslationTable{
  ["mirror_mode"]="镜像对决",
  [":mirror_mode"] = desc_oddfight_mode,

}

return kuanglang_mode